﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Proggy.Web.Cms.PropertyTypes;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using System.Web.Mvc;
using Proggy.Data.Abstract;
using Proggy.Web.Cms.Repositories.ContentModel;

namespace Proggy.Web.Cms.Core.PropertyTypes
{
    [Bind(Exclude = "Bytes")]
    [PropertyType("File uploader", "~/Areas/ProggyCore/Views/EditorPartials/ProggyFileUploaderEditor.cshtml")]
    public class ProggyFileUploader : PropertyTypeBase, IValidatablePropertyType
    {
        /// <summary>
        /// The posted file (for use in editor only)
        /// </summary>
        [JsonIgnore]
        public HttpPostedFileBase PostedFile { get; set; }

        /// <summary>
        /// The description of the file
        /// </summary>
        public string Description { get; set; }

        /// <summary>
        /// The mime type of the file
        /// </summary>
        public string MimeType
        {
            get;
            set;
        }

        /// <summary>
        /// The filename
        /// </summary>
        public string FileName
        {
            get;
            set;
        }

        /// <summary>
        /// The full path to the file (using "~/" at the start)
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// The server file path (eg C:\Inetpub\wwwroot ...etc)
        /// </summary>
        public string ServerFilePath { get; set; }

        /// <summary>
        /// Validate
        /// </summary>
        /// <param name="vContext"></param>
        /// <param name="attr"></param>
        /// <returns></returns>
        public Tuple<bool, string> GetIsValid(System.ComponentModel.DataAnnotations.ValidationContext vContext, ContentModel.ContentModelPropertyTypeAttribute attr)
        {
            if (string.IsNullOrEmpty(this.Description) || string.IsNullOrWhiteSpace(this.Description)) {
                return new Tuple<bool, string>(false, "Description for a file cannot be blank");
            }

            return new Tuple<bool, string>(true, "");
        }        
    }
}
